package com.edu.ucenterservice.controller;


import com.edu.common.utils.JwtUtils;
import com.edu.common.utils.R;
import com.edu.eduservicebase.exception.GuliException;
import com.edu.ucenterservice.entity.UcenterMember;
import com.edu.ucenterservice.service.UcenterMemberService;
import com.edu.ucenterservice.utils.ConstantPropertiesUtil;
import com.edu.ucenterservice.utils.HttpClientUtils;
import com.edu.ucenterservice.vo.LoginVo;
import com.edu.ucenterservice.vo.RegisterVo;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-07
 */
@Api("登录和注册管理")
@RestController
@RequestMapping("/api/ucenter")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;


    /**
     * 登录
     * @param loginVo
     * @return
     */
    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@ApiParam(value ="loginVo" ,name ="loginVo" ,required = true)
                   @RequestBody LoginVo loginVo){
        String token =ucenterMemberService.login(loginVo);
        return R.ok().data("token",token);
    }


    /**
     * 注册
     * @param RegisterVo
     * @return
     */
    @ApiOperation("注册")
    @PostMapping("/register")
    public R register(@ApiParam(value ="RegisterVo" ,name ="RegisterVo" ,required = true)
                      @RequestBody RegisterVo RegisterVo){

                ucenterMemberService.register(RegisterVo);
                return R.ok();
    }

    /**
     * 登录信息
     * @param request
     * @return
     */
    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("/auth/getLogin")
    public R getLoginInfo(HttpServletRequest request){

        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);

            RegisterVo registerVo = ucenterMemberService.getLoginInfo(memberId);

            return  R.ok().data("member",registerVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,"token错误!");
        }

    }





}

